﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="DoubleIntTable.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    12/03/2012 20:12 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using IveelySE.NLP.Common.DataStructure.List;

#endregion

namespace IveelySE.NLP.Common.DataStructure.Table
{
    /// <summary>
    ///   双精度-整型有序表
    /// </summary>
    [Serializable]
    public class DoubleIntTable : IntTable<double, double>
    {
        #region 属性或方法

        private readonly DoubleSortedList _list = new DoubleSortedList();

        #endregion

        #region 公有方法

        protected DoubleIntTable(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }

        public DoubleIntTable()
        {
        }

        public void AddItem(double item)
        {
            string[] items = item.ToString().Split('.');
            int integer = int.Parse(items[0]);
            double deci = 0.0;
            if(items.Length > 1)
            {
                deci = double.Parse(items[1]);
            }
            this.Add(deci, integer);
        }

        public void AddItems(double[] items)
        {
            foreach(double item in items)
            {
                this.AddItem(item);
            }
        }

        public void AddItems(List<double> items)
        {
            if(items!=null)
            {
                this.AddItems(items.ToArray());
            }
           
        }

        public SortedList<double, double> SortByKey()
        {
            foreach(DictionaryEntry de in this)
            {
                _list.Add(double.Parse(de.Key.ToString()), double.Parse(de.Value.ToString()));
            }

            return _list;
        }

        public IList<double> GetValuesBySort()
        {
            return this.SortByValue().Values;
        }

        #endregion

        #region 私有方法

        private DoubleSortedList SortByValue()
        {
            foreach(DictionaryEntry de in this)
            {
                _list.Add(double.Parse(de.Value.ToString()), double.Parse(de.Key.ToString()));
            }

            return _list;
        }

        #endregion
    }
}